package top.oneit.jdownload.controller.component;

import javafx.event.ActionEvent;
import javafx.event.Event;
import javafx.event.EventHandler;
import javafx.event.EventType;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.MenuItem;
import javafx.scene.control.*;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.input.ContextMenuEvent;
import javafx.scene.input.InputEvent;
import javafx.scene.input.MouseButton;
import javafx.scene.input.MouseEvent;
import top.oneit.jdownload.controller.BaseInitializable;
import top.oneit.jdownload.data.dao.DownloadingTablePoDao;
import top.oneit.jdownload.data.entity.DownloadingTablePo;
import top.oneit.jdownload.stages.HomeStage;

import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.datatransfer.Transferable;
import java.net.URL;
import java.util.ResourceBundle;

public class DownloadingTabController extends BaseInitializable {
    @FXML
    private TableView downloadingTableView;
    @FXML
    private TableColumn title, sizeStr, surplusStr, speed, actions;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        initTableColumn();

        downloadingTableView.setItems(getStage(HomeStage.class).downloadingTablePos);
        downloadingTableView.setOnContextMenuRequested(new EventHandler<ContextMenuEvent>() {
            @Override
            public void handle(ContextMenuEvent event) {
                System.out.println(event);
                // 选择的行
                int focusedIndex = downloadingTableView.getSelectionModel().getFocusedIndex();
                System.out.println(focusedIndex);
            }
        });

        ContextMenu contextMenu = new ContextMenu();
        // create menuitems
        MenuItem menuItem1 = new MenuItem("删除");
        MenuItem menuItem2 = new MenuItem("复制链接");
        MenuItem menuItem3 = new MenuItem("刷新表格");

        menuItem3.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                getStage(HomeStage.class).refreshTable();
            }
        });

        menuItem2.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Object selectedItem = downloadingTableView.getSelectionModel().getSelectedItem();
                DownloadingTablePo selected = (DownloadingTablePo) selectedItem;
                DownloadingTablePo po = DownloadingTablePoDao.get(DownloadingTablePo.class, selected.getId());
                System.out.println(po);
                Clipboard clip = Toolkit.getDefaultToolkit().getSystemClipboard();
                Transferable tText = new StringSelection(po.getDownloadFile().getUrl());
                clip.setContents(tText, null);
            }
        });

        menuItem1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Object selectedItem = downloadingTableView.getSelectionModel().getSelectedItem();
                DownloadingTablePo selected = (DownloadingTablePo) selectedItem;
                // 触发鼠标点击事件
                Label delete = (Label) selected.getActions().lookup("#delete");
                Event.fireEvent(delete,new MouseEvent(MouseEvent.MOUSE_CLICKED,
                        1,1,1,1, MouseButton.PRIMARY, 1,
                        true, true, true, true,
                        true, true, true,
                        true, true, true, null));
            }
        });

        // add menu items to menu
        contextMenu.getItems().add(menuItem1);
        contextMenu.getItems().add(menuItem2);
        contextMenu.getItems().add(menuItem3);

        // 添加表格的右键菜单
        downloadingTableView.setContextMenu(contextMenu);
    }

    private void initTableColumn() {
        //id.setCellValueFactory(new PropertyValueFactory<>("id"));
        title.setCellValueFactory(new PropertyValueFactory<>("title"));
        sizeStr.setCellValueFactory(new PropertyValueFactory<>("sizeStr"));
        surplusStr.setCellValueFactory(new PropertyValueFactory<>("surplusStr"));
        speed.setCellValueFactory(new PropertyValueFactory<>("speed"));
        actions.setCellValueFactory(new PropertyValueFactory<>("actions"));

        downloadingTableView.setPlaceholder(new Label("暂无下载内容"));
        title.prefWidthProperty().bind(downloadingTableView.widthProperty().multiply(0.4));
        sizeStr.prefWidthProperty().bind(downloadingTableView.widthProperty().multiply(0.14));
        surplusStr.prefWidthProperty().bind(downloadingTableView.widthProperty().multiply(0.14));
        speed.prefWidthProperty().bind(downloadingTableView.widthProperty().multiply(0.12));
        actions.prefWidthProperty().bind(downloadingTableView.widthProperty().multiply(0.2));

    }
}
